<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/12/28
 * Time: 16:00
 */

namespace app\api\controller;


use think\Db;

class Workorder extends Base
{
    /**
     * 工单列表
     * 展示我的，最近X月的工单
     * page  当前页码
     * @return \think\response\Json
     */
    public function workOrderList(){
        $params = input('param.');
        $is_exist = Db::table('up_factory_member')->where('m_id', $params['user_id'])->find();
        if(!$is_exist){
            return json(['status' => 1, 'msg' => '该用户不存在']);
        }
        //X个月之前
        $monthNum = Db::table('up_num_config')
            ->where('status', 2)
            ->value('num');
        $beforeTime = '-' . $monthNum . ' month';
        $dateTime = date('Y-m-d H:i:s', strtotime($beforeTime));
        
        $where['p_uid'] = $params['user_id'];
        $workOrder = Db::table('up_process')
            ->join('up_work_order', 'w_id = p_w_id', 'left')
            ->field('p_id, w_create_time, w_server_name, w_work_name, p_status, p_finish_name')
            ->whereTime('w_create_time', '>', $dateTime)
            ->where($where)
            ->page($params['page'], 10)
            ->order('p_status,w_update_time desc')
            ->select();
        if(!empty($workOrder)){
            return json(['status' => 200, 'msg' => $workOrder]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 工单详情
     * p_id  工单状态ID
     * @return \think\response\Json
     */
    public function workOrderDetail(){
        $params = input('param.');
        $is_exist = Db::table('up_factory_member')->where('m_id', $params['user_id'])->find();
        if(!$is_exist){
            return json(['status' => 1, 'msg' => '该用户不存在']);
        }
        $detail = Db::table('up_process')
            ->join('up_work_order', 'w_id = p_w_id', 'left')
            ->field('p_id, p_status, w_type, w_content')
            ->where('p_id', $params['p_id'])
            ->find();
        if(empty($detail)){
            return json(['status' => 0, 'msg' => '工单不存在']);
        }
        //如果工单详情是图片，需要转换成数组形式
        if($detail['w_type'] == 1){
            //转换成数组
            $arr_img = trim($detail['w_content'], ',');
            $detail['w_content'] = explode(',', $arr_img);
            //拼接url
            foreach($detail['w_content'] as $key => &$va){
                $va = http_type() . '/' . $va;
            }
        }else{
            $detail['w_content'] = http_type() . '/' . $detail['w_content'];
        }
        return json(['status' => 200, 'msg' => $detail]);
    }
    
    /**
     * 用户所在工厂中的所有人员
     * @return \think\response\Json
     */
    public function factoryUser(){
        $params = input('param.');
        $is_exist = Db::table('up_factory_member')->where('m_id', $params['user_id'])->find();
        if(!$is_exist){
            return json(['status' => 1, 'msg' => '该用户不存在']);
        }
        //该用户所在工厂ID
        $factory_id = Db::table('up_factory_member')
            ->where('m_id', $params['user_id'])
            ->value('m_factory_id');
        //该工厂中的所有人员
        $allUser = Db::table('up_factory_member')
            ->field('m_id, m_name')
            ->where('m_factory_id', $factory_id)
            ->select();
        if($allUser){
            return json(['status' => 200, 'msg' => $allUser]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 开始工单
     * p_id  加工状态ID
     * @return \think\response\Json
     */
    public function startWorkOrder(){
        $params = input('param.');
        $is_exist = Db::table('up_factory_member')->where('m_id', $params['user_id'])->find();
        if(!$is_exist){
            return json(['status' => 1, 'msg' => '该用户不存在']);
        }
        //更改状态
        $res = Db::table('up_process')->where('p_id', $params['p_id'])->setField('p_status', 2);
        if($res){
            return json(['status' => 200, 'msg' => '开始工单成功']);
        }else{
            return json(['status' => 200, 'msg' => '开始工单失败']);
        }
    }
    
    /**
     * 确认工单完成
     * finish_user_id  指定工单完成人的ID
     * p_id  工单加工状态ID
     * @return \think\response\Json
     */
    public function finishWorkOrder(){
        $params = input('param.');
        $is_exist = Db::table('up_factory_member')->where('m_id', $params['user_id'])->find();
        if(!$is_exist){
            return json(['status' => 1, 'msg' => '该用户不存在']);
        }
        //获取指定完成人名称
        $user_name = Db::table('up_factory_member')
            ->where('m_id', $params['finish_user_id'])
            ->value('m_name');
        //修改状态
        $data = [
            'p_finish_name' => $user_name,//指定完成人名称
            'p_status' => 3
        ];
        $res = Db::table('up_process')
            ->where('p_id', $params['p_id'])
            ->update($data);
        if($res){
            return json(['status' => 200, 'msg' => '确认工单完成成功']);
        }else{
            return json(['status' => 200, 'msg' => '确认工单完成失败']);
        }
    }
}